package at.fhj.swd.pse.domain;

import static org.junit.Assert.*;

import java.util.Date;
import java.util.Set;

import org.junit.Before;
import org.junit.Test;

import at.fhj.swd.pse.JpaTestBase;
import at.fhj.swd.pse.data.ActivityDAOMock;
import at.fhj.swd.pse.entities.Activity;
import at.fhj.swd.pse.entities.Community;
import at.fhj.swd.pse.entities.EActivityType;
import at.fhj.swd.pse.entities.User;

/**
 * @author Melvin Mawhin
 */

public class UTActivityService
	extends JpaTestBase {

	ActivityDAOMock m_dao;
	ActivityService m_service;
	Activity activity1;
	Activity activity2;
	Activity activity3;
	Community community;
	User user1;
	User user2;
	
    private Activity createNewActivity(EActivityType type, User owner, int id){
    	Activity activity = new Activity();
    	activity.setCreator(owner);
    	activity.setText("This is a test");
    	activity.setTimestamp(new Date());
    	activity.setType(type);
    	activity.setId(id);
		return activity;
    }
    
    private User createNewUser(String username, String password, int id){
    	User user = new User();
		user.setFirstName("A");
		user.setLastName("User");
		user.setAddress("Anyroad");
		user.setCity("Nowhere");
		user.setEmail("a.user@somenet.com");
		user.setUsername(username);
		user.setPassword(password);
		user.setId(id);
		return user;
    }
    
    private Community createNewCommunity(User user, int id){
    	Community community = new Community();
    	community.setAdministractor(user);
    	community.setDescription("test community");
    	community.setID(id);
    	community.setName("Test");
		return community;
  
    }
	
	@Before
	public void setUp() {
		m_dao = new ActivityDAOMock();
		m_service = new ActivityServiceImpl();
		m_service.setA_dao(m_dao);
		user1 = createNewUser("User1", "Test", 1);
		user2 = createNewUser("User2", "Test", 2);
		activity1 = createNewActivity(EActivityType.Friend, user2, 1);
		activity2 = createNewActivity(EActivityType.Global, user1, 2);
		activity3 = createNewActivity(EActivityType.Community, user1, 3);
		community = createNewCommunity(user1, 1);
		m_dao.addCommunity(community);
		m_dao.addUser(user1);
		m_dao.addUser(user2);
		Set<User> friends = user1.getFriends();
		friends.add(user2);
		user1.setFriends(friends);	
		m_service.createNewActivity(activity1);	
		m_service.createNewActivity(activity2);
		m_service.createNewActivity(activity3, community);
	}
	
	@Test
	public void getAllActivities(){
		assertEquals(3, m_service.getAllActivities(user1.getId()).size());

	}
	
	@Test
	public void removeActivity(){
		m_service.removeActivity(activity1);
		assertEquals(2, m_service.getAllActivities(user1.getId()).size());
	}
	
	@Test
	public void addActivity(){
		Activity test = createNewActivity(EActivityType.Friend, user1, 4);
		m_service.createNewActivity(test);
		assertEquals(4, m_service.getAllActivities(user1.getId()).size());
		
	}


}
